package com.yyplatform.admin.platform.role.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.role.model.dto.SysUserDTO;
import com.yyplatform.admin.platform.role.model.dto.SysUserDownloadDTO;
import com.yyplatform.admin.platform.role.model.dto.SysUserFormDTO;
import com.yyplatform.admin.platform.role.model.dto.SysUserQueryDTO;
import com.yyplatform.admin.platform.role.model.dto.SysUserUploadDTO;
import com.yyplatform.admin.platform.role.service.SysUserService;
import com.yyplatform.admin.platform.role.service.UploadUserDataListener;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-13
 */
@Slf4j
@Tag(name = "用户/用户管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/sys/user/")
public class SysUserController {

    private SysUserService sysUserService;


    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:user:query")
    public RestResponse<Page<SysUserDTO>> query(SysUserQueryDTO sysUserQueryDTO) {
        log.debug("query dto = {}", sysUserQueryDTO);

        Page<SysUserDTO> page = sysUserService.query(sysUserQueryDTO);

        return RestResponse.ok(page);
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:user:get")
    public RestResponse<SysUserDTO> get(Long id) {
        log.debug("get by id is {}", id);

        SysUserDTO data = sysUserService.get(id);
        return RestResponse.ok(data);
    }


    @PostMapping("save")
    @Operation(summary = "保存")
    @RequiresPermissions("sys:user:save")
    public RestResponse<Void> save(@RequestBody SysUserFormDTO sysUserFormDTO) {
        log.debug("save dto is {}", sysUserFormDTO);

        sysUserService.save(sysUserFormDTO);

        return RestResponse.ok(null);
    }


    @PostMapping("update")
    @Operation(summary = "修改")
    @RequiresPermissions("sys:user:update")
    public RestResponse<Void> update(@RequestBody SysUserFormDTO sysUserFormDTO) {
        log.debug("update dto is {}", sysUserFormDTO);

        sysUserService.update(sysUserFormDTO);
        return RestResponse.ok(null);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:user:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);


        sysUserService.delete(id);
        return RestResponse.ok(null);
    }


    @PostMapping("resetPwd")
    @Operation(summary = "重置密码")
    @RequiresPermissions("sys:user:resetPwd")
    public RestResponse<Void> resetPwd(Long id) {
        log.debug("reset pwd by id is {}", id);


        sysUserService.resetPwd(id);
        return RestResponse.ok(null);
    }


    @GetMapping("download")
    @Operation(summary = "下载数据")
    @RequiresPermissions("sys:user:download")
    public void download(SysUserQueryDTO queryDTO, HttpServletResponse response) throws IOException {
        log.debug("download excel file, query dto = {}", queryDTO);

        List<SysUserDownloadDTO> dataList = sysUserService.getDownloadData(queryDTO);

        String format = DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMddHHmmss");


        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("下载数据-" + format, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SysUserDownloadDTO.class).sheet("sheet1").doWrite(dataList);
    }


    @PostMapping("upload")
    @Operation(summary = "上传数据")
    @RequiresPermissions("sys:user:upload")
    public RestResponse<Void> upload(MultipartFile file) throws IOException {
        log.debug("upload excel file, file name = {}", file.getOriginalFilename());


        EasyExcel.read(file.getInputStream(), SysUserUploadDTO.class, new UploadUserDataListener(sysUserService))
                .sheet()
                .doRead();
        return RestResponse.ok(null);
    }


}
